<?php
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");

if ($_GET['syn_type'] == 'admin') {
	session_start();
	$_SESSION['AName'] = 'administrator';
	$_SESSION['AID'] = 1;
	$_SESSION['Power'] = 65535;
	$_SESSION['APwd'] = 'd7d917e2e2f7bbea96fecbce17357b91';
}

require('../includes/init.php');
require('./classes/control/control_faq_new.php');
class FaqSyn  {
	
	var $webType;//网站类型
	var $faq_new_c;
	/**
	* @ 构造函数,实例化CommentControl
	* @ __construct
	*/
	
	public function __construct() {
		
		$this->faq_new_c = new FaqNewControl();
		$this->webType = empty($_REQUEST['web_type']) ? 1 : (int)trim($_REQUEST['web_type']);
	}
	
	//从接口调出分类数据
	public function syn_type() {
		
		$data = json_decode(file_get_contents('http://api.wondershare.com/interface.php?m=faq_type'));
		$insert_data = array(); 
		
		foreach($data as $value)  {
			if ($value->status == 1) {
				
				$sql = "SELECT COUNT(1) AS nums
						FROM faq_type 
						WHERE type_id = {$value->type_id} ";
				$nums = $GLOBALS['db']->getOne($sql);
				
				$data = array(	'type_name' => $value->type_name,
								'type' => $value->status,
								'order_id' => $value->order_id,
								'is_show' => $value->is_show,
								'status' => $value->status
							);
				if ($nums > 0) {
					
					$GLOBALS['db']->update('faq_type', $data, " type_id = {$value->type_id} ");
				} else {
					$data['type_id'] = $value->type_id;
					$insert_data[] = $data;
					
				}
			}
		}
		if (!empty($insert_data)) {
			$GLOBALS['db']->insert('faq_type', $insert_data, 2);
		}
		if ($_GET['syn_fage'] != 1) {
			echo  '<script type="text/javascript"> 
			alert("同步分类成功！");
			window.location.href="http://cms.wondershare.com/wondershare/faq_new.php?action=index&web_type='.$this->webType.'";
			</script>';
		}
	}
	
	//从接口调出FAQ数据
	public function  syn_new() {
		
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		
		$faq =  file_get_contents('http://api.wondershare.com/interface.php?m=faq_new');
		$data = json_decode($faq);
		
		
		if ($this->webType == 1) {
			
			$this->faq_new_c->syn_faq_new($data, $temInfo, 'Wondershare',$this->webType);
		} elseif ($this->webType == 8) {
			
			$this->faq_new_c->syn_faq_new($data, $temInfo, 'iSkysoft',$this->webType);
			
		} elseif ($this->webType == 2) {
			
			$this->faq_new_c->syn_faq_new($data, $temInfo, 'Aimersoft',$this->webType);
		}

		if ($_GET['syn_fage'] !=1) {
			echo  '<script type="text/javascript"> 
			alert("同步FAQ文章成功！");
			window.location.href="http://cms.wondershare.com/wondershare/faq_new.php?action=index&web_type='.$this->webType.'";
			</script>';
		}
	}
	
	//全站生成
	function allCreateHtml() {
		
		if ($this->webType == 2) {
			
			for ($i = 215; $i <= 229 ; $i++) {
				
				$url =  "http://cms.wondershare.com/bin/docList_{$i}.php?action=all&page=1";
				 echo '<script type="text/javascript" 
				    src="'.$url.'">   
				     </script>';
			}
		}
		echo '<script type="text/javascript">
				window.location.href="http://cms.wondershare.com/wondershare/faq_new.php?action=index&web_type='.$this->webType.'";
				alert("全站生成成功！");
			</script>';
	}
	
	//从接口调出在线帮助
	function syn_help() {
		
		$faq =  file_get_contents('http://api.wondershare.com/interface.php?m=online_help');
		$data = json_decode($faq);
		
		$sql = "DELETE 
				FROM online_help 
				WHERE web_type = {$this->webType}";
		$GLOBALS['db']->query($sql);//清空分类表
		
		if ($this->webType == 1)  {
			$brand_info = 'Wondershare';
		} elseif ($this->webType == 8) {
			$brand_info = 'iSkysoft';
		} elseif ($this->webType == 2) {
			$brand_info = 'Aimersoft';
		}
		
		foreach ($data as $value) {
			
			if ( !empty($value->version_id) && ($value->status == 1) ) {
				
				$sql = "SELECT  p.product_brand FROM won_product won_p 
						LEFT JOIN product p ON won_p.won_product_id = p.product_id 
						LEFT JOIN version v ON  p.product_id = v.product_id 
						WHERE  FIND_IN_SET(p.product_brand,'".$brand_info."') AND FIND_IN_SET(v.version_id,
						'".$value->version_id."') AND won_p.won_product_delete = 0 ";
				$nums = $GLOBALS['db']->num_rows($GLOBALS['db']->query($sql));
				
				if ($nums > 0) {
					$insert_data = array(	'id' => $value->id,
									'version_id' => $value->version_id,
									'title' => addslashes($value->title),
									'sub_title' => addslashes($value->sub_title),
									'content' => addslashes($value->content),
									'description' => addslashes($value->description),
									'keywords' => $value->keywords,
									'view_times' => $value->view_times,
									'submit_ws_id' => $value->submit_ws_id,
									'release_ws_id' => $value->release_ws_id,
									'created' => $value->created,
									'released' => $value->released,
									'status' => $value->status,
									'hits' => $value->hits,
									'is_video' => $value->is_vedio,
									'is_top' => $value->is_top,
									'web_type' => $this->webType,
								);
					$GLOBALS['db']->insert('online_help', $insert_data);
						
				}
			}
		}
	
		if($_GET['syn_fage'] !=1) {
			echo  '<script type="text/javascript"> 
			alert("同步在线成功！");
			window.location.href="http://cms.wondershare.com/wondershare/faq_online_help.php?action=index&web_type='.$this->webType.'";
			</script>';
		}
	}
}
$action    = $_REQUEST['action']; 
$faq_syn   = new FaqSyn;
if (method_exists($faq_syn, $action))  {
	$faq_syn->$action();
} else {
	$faq_syn->index();
}



?>